package com.ruibang.glass.quality.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.domain.req.QualityDarkroomReq;
import com.ruibang.glass.quality.entity.GrindRange;
import com.ruibang.glass.quality.entity.QualityDarkroom;
import com.ruibang.glass.quality.mapper.QualityDarkroomMapper;
import com.ruibang.glass.quality.service.QualityDarkroomService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 成品暗房信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-21
 */
@Service
public class QualityDarkroomServiceImpl extends ServiceImpl<QualityDarkroomMapper, QualityDarkroom> implements QualityDarkroomService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Override
    public PageResult<QualityDarkroom> queryPage(PageRequest<QualityDarkroomReq> pageRequest) {
        final QualityDarkroomReq param = pageRequest.getParam();
        final LambdaQueryWrapper<QualityDarkroom> lambda = new QueryWrapper<QualityDarkroom>().lambda();
        this.buildCondition(lambda,param);
        final IPage<QualityDarkroom> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        if(page!=null&&CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            for (QualityDarkroom record : page.getRecords()) {
                if(userMap!=null){
                    record.setUserName(userMap.get(record.getUserId())==null?"":userMap.get(record.getUserId()).toString());
                }
            }
        }

        PageResult<QualityDarkroom> pr = new PageResult(page);
        return pr;
    }


    @Override
    public void delete(String id) {
        lambdaUpdate().eq(QualityDarkroom::getDarkroomId,id).set(QualityDarkroom::getIsDelete, Constants.ONE_STR).update();
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<QualityDarkroom> lambda, QualityDarkroomReq param){
        lambda.eq(QualityDarkroom::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(StringUtils.isNotBlank(param.getDarkroomId())){
            lambda.eq(QualityDarkroom::getDarkroomId, param.getDarkroomId());
        }
        if(StringUtils.isNotBlank(param.getGlassId())){
            lambda.eq(QualityDarkroom::getGlassId, param.getGlassId());
        }
        if(StringUtils.isNotBlank(param.getGlassModel())){
            lambda.eq(QualityDarkroom::getGlassModel, param.getGlassModel());
        }
        if(param.getCheckStartTime()!=null&&param.getCheckEndTime()!=null){
            lambda.ge(QualityDarkroom::getCheckTime, param.getCheckStartTime());
            lambda.le(QualityDarkroom::getCheckTime, param.getCheckEndTime());
        }
        if(StringUtils.isNotBlank(param.getProductLine())){
            lambda.eq(QualityDarkroom::getProductLine, param.getProductLine());
        }
        if(StringUtils.isNotBlank(param.getUserId())){
            lambda.eq(QualityDarkroom::getUserId, param.getUserId());
        }
        if(StringUtils.isNotBlank(param.getDecide())){
            lambda.eq(QualityDarkroom::getDecide, param.getDecide());
        }

        if(StringUtils.isNotBlank(param.getCheckSpray())){
            lambda.eq(QualityDarkroom::getCheckSpray, param.getCheckSpray());
        }
        lambda.orderBy(true,false, QualityDarkroom::getCheckTime);
    }
}
